package com.hbhmzy.contact;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hbhmzy.call.CallingActivity;
import com.hbhmzy.call.R;

import com.hbhmzy.utils.Client;


public class ContactDetailActivity extends Activity implements OnClickListener {
    private static final int EDIT_REQUEST_CODE = 1;
    private TextView tvName;
    private TextView tvNumber;
    private String name;
    private String number;
    private String thisphone;
    public static String targetnumber;
    public static String symbol;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.contact_detail_info);
        init();

    }

    private void init() {
        ImageView back = findViewById(R.id.root_back4);
        TextView title = findViewById(R.id.root_title4);
        title.setText("详情");
        ImageView edit = findViewById(R.id.root_edit);
        tvName = findViewById(R.id.contact_name);
        tvNumber = findViewById(R.id.contact_number);
        TextView call = findViewById(R.id.contact_call);
        TextView msg = findViewById(R.id.contact_msg);

        // 在界面上显示联系人姓名和号码
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        number = intent.getStringExtra("number");
        tvName.setText(name);
        tvNumber.setText(number);

        back.setOnClickListener(this);
        edit.setOnClickListener(this);
        call.setOnClickListener(this);
        msg.setOnClickListener(this);

    }

    //重写返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.root_back4: // 返回
                finish();
                break;

            case R.id.root_edit: // 编辑
                Intent editIntent = new Intent(this, ContactEditActivity.class);
                editIntent.putExtra("name", name);
                editIntent.putExtra("number", number);
                startActivityForResult(editIntent, EDIT_REQUEST_CODE);
                break;

            case R.id.contact_call: // 加密通话
//			Uri diaUri = Uri.parse("tel:" + number);
//			Intent diaIntent = new Intent(Intent.ACTION_DIAL, diaUri);
//			startActivity(diaIntent);
                targetnumber = number;
                if (targetnumber.equals("")) {
                    Toast.makeText(ContactDetailActivity.this, "请您先输入拨打的用户电话", Toast.LENGTH_SHORT).show();
                } else if (targetnumber.length() < 11) {
                    Toast.makeText(ContactDetailActivity.this, "请输入有效的电话号码", Toast.LENGTH_SHORT).show();
                } else {

                    //获取Shared Preference对象
                    SharedPreferences pref=getPreferences(Activity.MODE_PRIVATE);
                    //取出保存的用户名和密码分别赋给字符串String username,password
                    thisphone = pref.getString("name","");
                    Log.i("name",thisphone);
                    symbol = thisphone + "#" + targetnumber + ":";// 1#2:  或者 2#1:标记
                    Intent intent1 = new Intent(ContactDetailActivity.this, CallingActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("is_online", false);
                    bundle.putString("symbol", symbol);
                    intent1.putExtras(bundle);
                    startActivity(intent1);
                    Client.send(symbol + "start");
                }
                break;

            case R.id.contact_msg: // 加密聊天
                Uri msgUri = Uri.parse("smsto:" + number);
                Intent msgIntent = new Intent(Intent.ACTION_SENDTO, msgUri);
                startActivity(msgIntent);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        switch (requestCode) {
            case EDIT_REQUEST_CODE:
                if (resultCode == Constant.EDIT_RESULT_CODE) {//表示编辑成功了
                    name = intent.getStringExtra("name");
                    number = intent.getStringExtra("number");
                    tvName.setText(name);
                    tvNumber.setText(number);
                    setResult(Constant.EDIT_RESULT_CODE);
                } else if (resultCode == Constant.DELETE_RESULT_CODE) {//删除成功
                    setResult(Constant.DELETE_RESULT_CODE);
                    finish();
                }
                break;
        }
    }

}
